package com.xy.modulemain.viewpage

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.ToastUtils
import com.xy.modulemain.R
import com.xy.modulemain.adapter.RecyclerViewAdapter
import com.xy.modulemain.bean.Item
import com.xy.modulemain.bean.PagingResult
import com.xy.modulemain.lisener.IBaseModelListener
import com.xy.modulemain.model.ItemModel
import com.xy.network.bean.ResponseBean
import kotlinx.android.synthetic.main.item_fragment.*

/**
 * @ClassName ItemFragment
 * @Author xy
 * @Date 2021/8/20 9:49
 * @Description TODO
 */
const val ItemId = "itemId"
const val ItemName = "itemName"
class ItemFragment():Fragment(),IBaseModelListener<ResponseBean<MutableList<Item>>> {
    constructor(itemId:String, itemName:String):this(){
        this.arguments?.putString(ItemId,itemId)
        this.arguments?.putString(ItemName,itemName)
    }

    lateinit var adapter:RecyclerViewAdapter
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.item_fragment,container,false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        refreshLayout.autoRefresh()
         adapter = activity?.let { RecyclerViewAdapter(it) }!!
        recyclerView.adapter = adapter
        val itemModel = ItemModel(arguments?.getString(ItemId,"").toString(),arguments?.getString(ItemName,"").toString())
        //刷新
        refreshLayout.setOnRefreshListener {
//            pageIndex = 1
//            setData()
            itemModel.refresh()
        }

        //加载
        refreshLayout.setOnLoadMoreListener {
//            setData()
            itemModel.load()
        }
    }


    var itemList = mutableListOf<Item>()
    override fun onLoadFinish(t: ResponseBean<MutableList<Item>>, vararg pagingResult: PagingResult) {
        if (pagingResult[0].isRefresh){
            itemList.clear()
        }
        itemList.addAll(t.data)
        adapter.setData(itemList)
        if (pagingResult[0].hasNextPage){
            refreshLayout.finishRefresh()
            refreshLayout.finishLoadMore()
        }else{
            refreshLayout.finishLoadMoreWithNoMoreData() //没有更多数据
        }

    }

    override fun onLoadFailure(promat: String) {
        refreshLayout.finishRefresh(false)
        refreshLayout.finishLoadMore(false)
        ToastUtils.showShort(promat)
    }
//    private fun setData(){
//        MainNetWorkApi.getService(ApiService::class.java)?.getList(pageIndex,11)
//            ?.compose(MainNetWorkApi.instance.applySchedulers(object :BaseObserver<ResponseBean<MutableList<Item>>>{
//                override fun onSuccess(t: ResponseBean<MutableList<Item>>) {
//                    if (pageIndex==1){
//                        itemList.clear()
//                    }
//                    itemList.addAll(t.data)
//                    adapter?.setData(itemList)
//                    pageIndex++
//                    refreshLayout.finishRefresh()
//                    refreshLayout.finishLoadMore()
//                }
//
//                override fun onFailure(e: Throwable) {
//                    ToastUtils.showShort(e.message)
//                    refreshLayout.finishRefresh(false)
//                    refreshLayout.finishLoadMore(false)
//                }
//
//            }))
//
//    }
}